package com.application.module_chat2.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.application.module_chat2.R;
import com.application.mylibrary.im.MsgType;
import com.application.mylibrary.utils.DateUtils;
import com.application.mylibrary.widget.keybord.utils.EmojiUtil;

import java.util.List;

import io.rong.imlib.model.Message;

/**
 * Created by lixuce on 2019/8/8.
 */
public class ChatAdapter extends BaseMultiItemQuickAdapter<ChatAdapter.ChatBean, BaseViewHolder> {


    public ChatAdapter(List data) {
        super(data);
        addItemType(MsgType.TYPE_MSG_SELF, R.layout.item_im_text_me);
        addItemType(MsgType.TYPE_MSG_OTHER, R.layout.item_im_text_other);
    }

    @Override
    protected void convert(BaseViewHolder helper, ChatBean item) {

        TextView timeView = null;
        if (helper.getItemViewType() == 0) {
            return;
        }
        switch (helper.getItemViewType()) {
            case MsgType.TYPE_MSG_SELF:
                timeView = helper.getView(R.id.text_chat_item_time);
                bindSelfMessage(helper, item);
                break;
            case MsgType.TYPE_MSG_OTHER:
                timeView = helper.getView(R.id.text_chat_item_time);
                bindOtherMessage(helper, item);
                break;
        }
        bindCommont(helper, item, timeView);
    }

    //设置别人发送的消息
    private void bindOtherMessage(BaseViewHolder helper, ChatBean bean) {
        //设置聊天界面男女聊天框背景

        helper.setText(R.id.text_chat_item_text, EmojiUtil.getInstance().handlerEmojiText(mContext, bean.getMessage()))
                .addOnLongClickListener(R.id.text_chat_item_text)
                .addOnClickListener(R.id.img_chat_item_avater)//头像设置点击事件
                .setVisible(R.id.tv_user_name, true)//设置别人用户名显示
                .setText(R.id.tv_user_name, bean.getUserName());//设置别人用户名;
    }

    //设置自己发送的消息
    private void bindSelfMessage(BaseViewHolder helper, ChatBean item) {


        ImageView avater = helper.getView(R.id.img_chat_item_avater);
//        ImageUtil.displayCircleImageNoAnim(avater, UrlManager.getRealHeadPath(item.getUserPic()));
        helper.setText(R.id.text_chat_item_text, EmojiUtil.getInstance().handlerEmojiText(mContext, item.getMessage()))
                .setVisible(R.id.img_chat_item_failed, item.sentStatus == Message.SentStatus.FAILED)
                .addOnClickListener(R.id.img_chat_item_failed)//重新发送按钮
                .addOnLongClickListener(R.id.text_chat_item_text);


    }

    //设置公共部分的UI 时间
    private void bindCommont(BaseViewHolder helper, ChatBean item, TextView timeView) {
        if (timeView == null) {
            return;
        }
        int position = helper.getAdapterPosition();
        long lastTime;
        if (position == 0) {
            lastTime = -1;
        } else {
            ChatBean beforItem = getItem(position - 1);
            if (beforItem != null) {
                lastTime = beforItem.getTime();
            } else {
                lastTime = -1;
            }

        }
        showTime(item.getTime(), lastTime, timeView);

    }

    //显示时间
    private void showTime(long currentTime, long lastTime, TextView timeView) {
        if (timeView == null) {
            return;
        }
        String time = DateUtils.formatData(currentTime, mContext.getResources().getString(R.string.rc_yesterday_format));
        timeView.setText(time);
        if (lastTime == -1) {
            timeView.setVisibility(View.VISIBLE);
        } else {
            if (DateUtils.isShowChatTime(currentTime, lastTime, 60)) {
                timeView.setVisibility(View.VISIBLE);
            } else {
                timeView.setVisibility(View.GONE);
            }
        }
    }

    public void addMessage(ChatBean chatBean) {
        addData(chatBean);
    }

    public void addDataBeanList(List<ChatBean> beans) {
        addData(0, beans);
    }
    //通过消息id 获取到当前索引
    public int getPositionByMessageId(int msgId) {
        for (int i = 0; i < getData().size(); i++) {
            ChatBean bean = getData().get(i);
            if (msgId == (bean.getMessageId())) {
                return i;
            }
        }
        return -1;
    }
    public static class ChatBean implements MultiItemEntity {

        private int msgType;
        private String message = "";
        private String sysMessage = "";
        private String gifUrl = "";
        private String userName = "";
        private String userPic = "";
        private String userId = "";
        private int sex;
        private long time;
        private int messageId;
        private String backMessage;
        private String toUserId;
        private String toUserName;

        public String getToUserName() {
            return toUserName;
        }

        public void setToUserName(String toUserName) {
            this.toUserName = toUserName;
        }

        public String getBackMessage() {
            return backMessage;
        }

        public void setBackMessage(String backMessage) {
            this.backMessage = backMessage;
        }

        public String getToUserId() {
            return toUserId;
        }

        public void setToUserId(String toUserId) {
            this.toUserId = toUserId;
        }

        public Message.SentStatus sentStatus = Message.SentStatus.SENT;
        private boolean isWelcome;

        public boolean isWelcome() {
            return isWelcome;
        }

        public void setWelcome(boolean welcome) {
            isWelcome = welcome;
        }

        @Override
        public int getItemType() {
            return msgType;
        }


        public int getMsgType() {
            return msgType;
        }

        public void setMsgType(int msgType) {
            this.msgType = msgType;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getSysMessage() {
            return sysMessage;
        }

        public void setSysMessage(String sysMessage) {
            this.sysMessage = sysMessage;
        }

        public String getGifUrl() {
            return gifUrl;
        }

        public void setGifUrl(String gifUrl) {
            this.gifUrl = gifUrl;
        }

        public String getUserName() {
            return userName == null ? "" : userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getUserPic() {
            return userPic == null ? "" : userPic;
        }

        public void setUserPic(String userPic) {
            this.userPic = userPic;
        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public int getSex() {
            return sex;
        }

        public void setSex(int sex) {
            this.sex = sex;
        }

        public long getTime() {
            return time;
        }

        public void setTime(long time) {
            this.time = time;
        }

        public int getMessageId() {
            return messageId;
        }

        public void setMessageId(int messageId) {
            this.messageId = messageId;
        }
    }
}
